package com.teach.basic.service.impl;

import com.common.bean.PageVo;
import com.common.bean.QueryCondition;

import com.common.exception.RDException;
import com.common.utils.BeanUtil;
import com.common.utils.PageUtil;
import com.teach.basic.model.dto.ArticleDTO;
import com.teach.basic.model.po.Article;
import com.teach.basic.dao.ArticleDao;
import com.teach.basic.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * <p>
 *  文章表 服务实现类
 * </p>
 * @version 1.0
 * @author hzz
 * @since 2020-03-10
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleDao, Article> implements ArticleService {
    @Autowired
    private ArticleDao articleDao;
    @Transactional(rollbackFor = RDException.class)
    public String add(ArticleDTO articleDTO){
        Article article=new Article();
        BeanUtil.copy(articleDTO,article);
        this.baseMapper.insertSelective(article);
        if(article.getId()==null){
            throw new RDException("插入失败");
        }
        return article.getId();
    }

    public PageVo queryPage(QueryCondition params,Article r){
        String [] order={"passtime"};
        if( params.getOrder()!=null && Arrays.binarySearch(order, params.getOrder())<0 ){
            params.setOrder(null);
        }
        PageVo pageVo = PageUtil.selectPage(() -> articleDao.findArticle(r), params);
        return pageVo;
    }
}
